﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Entities;
using BaHuan.DataLayer;
using BaHuan.Common;
using BaHuan.BusinessLayer;

namespace BaHuan
{
    public partial class FrmTimKiemMuaHang : Form
    {
        private MuaHangManagement muahangMgt = new MuaHangManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemMuaHang(NhanVien nhanvien)
        {
            InitializeComponent();
            this.NhanVien = nhanvien;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            FrmMuaHang frmMuaHang = new FrmMuaHang(this.nhanvien);
            this.Hide();
            frmMuaHang.ShowDialog();
            this.Show();
        }

        private void FrmTimKiemMuaHang_Load(object sender, EventArgs e)
        {
            dgvTimKiemMuaHang.AutoGenerateColumns = false;
            dpDenNgay.Text = DateTime.Now.Date.ToShortDateString();
        }

        private void btnTimKiemMuaHang_Click(object sender, EventArgs e)
        {
            dgvTimKiemMuaHang.DataSource = null;
            DateTime tungay = dpTuNgay.Value;
            DateTime denngay = dpDenNgay.Value;

            if (denngay < tungay)
            {
                MessageBox.Show("Thời gian tìm kiếm không hợp lệ, vui lòng kiểm tra lại!");
                return;
            }

            List<MuaHang> lstMuaHang = (from mh in GetAllMuaHang()
                                        where (mh.MaSo.ToLower().Contains(txtTimKiemMuaHang.Text.ToLower()) ||
                                            mh.NguoiLap.ToLower().Contains(txtTimKiemMuaHang.Text.ToLower())) &&
                                            mh.NgayThang.Date >= tungay.Date && mh.NgayThang.Date <= denngay.Date
                                        select mh).ToList();
            if (lstMuaHang.Count > 0)
            {
                dgvTimKiemMuaHang.DataSource = lstMuaHang;
            }

            if (dgvTimKiemMuaHang.DataSource == null)
            {
                MessageBox.Show("Không tìm thấy kết quả cần tìm","Thông báo!");
                return;
            }
        }

        public List<MuaHang> GetAllMuaHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = muahangMgt.GetAllMuaHang();
            List<MuaHang> listMuaHang = (from mh in(List<MuaHang>)resultObj.Result
                                             where mh.IsDelete==0
                                             select mh).ToList();

            return listMuaHang;
        }

        private void dgvTimKiemMuaHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
             MuaHang muahang = (MuaHang)dgvTimKiemMuaHang.Rows[e.RowIndex].DataBoundItem;
             FrmMuaHang frmMuaHang = new FrmMuaHang(muahang,nhanvien);
             this.Hide();
             frmMuaHang.ShowDialog();
             this.Show();
             dgvTimKiemMuaHang.DataSource = null;
        }
    }
}
